package znbkListRdtz.adapter;

import android.animation.Animator;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.lancooznbk.R;
import znbkListRdtz.entity.HeatPointExtensionListEntity;
import com.example.lancooznbk.utils.AnimatorHelper;
import znbkListRdtz.fragment.HeatPointExtensionListFragment;
import com.example.lancooznbk.view.widget.RatingBar;
import com.example.lancooznbk.view.widget.RippleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018\2\5 0005.
 */

public class HeatPointExtensionListAdapter extends BaseRecyclerAdapter<HeatPointExtensionListAdapter.ViewHolder>{
    private Context mContext;
    private List<HeatPointExtensionListEntity.ResourceBean> mList;
    private OnItemClickListener onItemClickListener;

    private int mLastPosition = -1;

    public HeatPointExtensionListAdapter(Context context) {
        this.mContext = context;
        mList = new ArrayList<>();
    }

    public HeatPointExtensionListAdapter(Context mContext, List<HeatPointExtensionListEntity.ResourceBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public  interface OnItemClickListener{
        void onItemClick(View view, List<HeatPointExtensionListEntity.ResourceBean> list, int position);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder getViewHolder(View view) {

        return new ViewHolder(view,false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_rv_rdtz, parent,false);
        return new ViewHolder(view,true);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,  int position, boolean isItem) {

        holder.rippleLayout.setTag(position);
        //设置波纹效果
        holder.rippleLayout.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView v) {
                if (onItemClickListener != null){
                    onItemClickListener.onItemClick(v,mList, (Integer) v.getTag());
                }

            }
        });

       /* holder.paperInto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if (onItemClickListener != null){
                  onItemClickListener.onItemClick(v,mList, (Integer) v.getTag());
              }
            }
        });*/

        HeatPointExtensionListEntity.ResourceBean itemBean = mList.get(position);

        String ResType = itemBean.getResType();
        if(ResType.equals("1")){
            holder.ivResourceType.setImageResource(R.mipmap.znbk_rdtz_icon_text);
            holder.rippleLayout.setBackgroundResource(R.mipmap.znbk_rdtz_item_blue_bg);
        }else if(ResType.equals("2")){
            holder.ivResourceType.setImageResource(R.mipmap.znbk_rdtz_icon_picture);
            holder.rippleLayout.setBackgroundResource(R.mipmap.znbk_rdtz_item_blue_bg);
        }else if(ResType.equals("5")){
            holder.ivResourceType.setImageResource(R.mipmap.znbk_rdtz_icon_audio);
            holder.rippleLayout.setBackgroundResource(R.mipmap.znbk_rdtz_item_purple_bg);
        }else if(ResType.equals("6")){
            holder.ivResourceType.setImageResource(R.mipmap.znbk_rdtz_icon_video);
            holder.rippleLayout.setBackgroundResource(R.mipmap.znbk_rdtz_item_purple_bg);
        }else if(ResType.equals("Q")){
            holder.ivResourceType.setImageResource(R.mipmap.znbk_rdtz_icon_test_question);
            holder.rippleLayout.setBackgroundResource(R.mipmap.znbk_rdtz_item_green_bg);
        }

        holder.paperInduction.setText(itemBean.getTitle());

        /*
        * 推荐人气
        * */
        int hotIndex = itemBean.getHotIndex();
        String recommendPersonTime = String.format(mContext.getString(R.string.rdtz_recommendPersonTime), String.valueOf(hotIndex));
        holder.mTvRecommendPersonTime.setText(recommendPersonTime);

        /*
        * 推荐时间
        * */
        String recommendTime = itemBean.getUpdateTime().substring(0,10).replaceAll("-",".");
        recommendTime = String.format(mContext.getString(R.string.rdtz_recommendTime), recommendTime);
        holder.mTvRecommendTime.setText(recommendTime);

        /*
        * 学习时间
        * */
        String LearnedTime = itemBean.getLearnedTime();
        if(!LearnedTime.equals("")) {
            LearnedTime = String.format(mContext.getString(R.string.rdtz_learnedTime), LearnedTime.substring(0, 10).replaceAll("-","."));
        }else{
            LearnedTime = String.format(mContext.getString(R.string.rdtz_learnedTime),"暂未学习");
        }
        holder.mTvLearnedTime.setText(LearnedTime);

        int adapterPosition = holder.getAdapterPosition();

        boolean isScrollDown = adapterPosition > mLastPosition;

        for (Animator anim : AnimatorHelper.getRecyclerAnimator(holder.itemView,isScrollDown)) {
            anim.start();
        }
        mLastPosition = adapterPosition;
    }

    @Override
    public int getAdapterItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private RippleView rippleLayout;
        private TextView paperInduction;//资料简介
        private ImageView ivResourceType;//资源类型
        private TextView mTvRecommendPersonTime;//推荐人气
        private TextView mTvRecommendTime;//推荐时间
        private TextView mTvLearnedTime;//推荐时间

        public ViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem){
                //波纹效果
                rippleLayout = (RippleView) itemView.findViewById(R.id.item_ripper);
                paperInduction = (TextView) itemView.findViewById(R.id.tv_induction);
                ivResourceType = (ImageView) itemView.findViewById(R.id.iv_resource_type);
                mTvRecommendPersonTime = (TextView) itemView.findViewById(R.id.tv_RecommendPersonTime);
                mTvRecommendTime = (TextView) itemView.findViewById(R.id.tv_RecommendTime);
                mTvLearnedTime = (TextView) itemView.findViewById(R.id.tv_learnedTime);
            }
        }
    }

    public void add(List<HeatPointExtensionListEntity.ResourceBean> data){
        int startPosition = getStart()+getAdapterItemCount();
        mList.addAll(data);
        notifyItemRangeInserted(startPosition,data.size());

    }

    public void clearAndAdd(List<HeatPointExtensionListEntity.ResourceBean> data){
        mLastPosition = -1;
        mList.clear();
        mList.addAll(data);
        notifyDataSetChanged();
    }
}
